//
//  WDWorderOrderReviewVehicleViewModel.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/8.
//

#import "WDWorderOrderReviewVehicleViewModel.h"

@implementation WDWorderOrderReviewVehicleViewModel

- (void)initialize {
    [super initialize];
    
    self.title = @"工单界面";
    
    _reqModel = [WDTaskFinishInspectReqModel new];
    _reqModel.engineerId = @([EDSAppDelegateTool sharedInstance].userInfoModel.id);
    _reqModel.stationId = @([EDSAppDelegateTool sharedInstance].userInfoModel.stationId);
    _reqModel.files = [NSMutableArray new];
    
    
    _processModes = [NSMutableArray new];
    TYZCommonCellModel *model = [TYZCommonCellModel new];
    model.title = @"直接维修";
    model.isCheck = NO;
    [_processModes addObject:model];
    
    model = [TYZCommonCellModel new];
    model.title = @"更换备件";
    model.isCheck = NO;
    [_processModes addObject:model];
    
    model = [TYZCommonCellModel new];
    model.title = @"调件维修";
    model.isCheck = NO;
    [_processModes addObject:model];
    
    
    
    _remarkImageInfos = [NSMutableArray new];
    model = [TYZCommonCellModel new];
    model.title = @"车辆VIN码照片";
    model.isMuist = YES;
    [_remarkImageInfos addObject:model];
    
    model = [TYZCommonCellModel new];
    model.title = @"车辆仪表照片";
    model.iconImgName = @"ic_veh_dashboard_default";
    model.isMuist = YES;
    [_remarkImageInfos addObject:model];
    
    model = [TYZCommonCellModel new];
    model.title = @"车头45°照片";
    model.iconImgName = @"ic_vehicle_default";
    model.isMuist = YES;
    [_remarkImageInfos addObject:model];
    
    model = [TYZCommonCellModel new];
    model.title = @"故障部位照片";
    model.iconImgName = @"ic_vehicle_default";
    model.isMuist = YES;
    [_remarkImageInfos addObject:model];
    
    model = [TYZCommonCellModel new];
    model.title = @"行驶证信息照片";
    model.iconImgName = @"ic_driving_license";
    model.isMuist = YES;
    [_remarkImageInfos addObject:model];
    
    
    _addImageVideoNum = 4;
    
}

- (void)setupData {
    _reqModel.engineerId = @([EDSAppDelegateTool sharedInstance].userInfoModel.id);
    _reqModel.stationId = @([EDSAppDelegateTool sharedInstance].userInfoModel.stationId);
    _reqModel.taskId = @(self.serviceModel.id);
    
}


- (float)imageWH
{
    if (_imageWH)
    {
        return _imageWH;
    }
    
    _imageWH = (kScreenWidth - 16 * 2 - 4 * 3) / 4.0;
    return _imageWH;
}

//- (NSString *)dynamicContent
//{
//    return [_dynamicContent stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//}

/**
 得到图片的大小
 
 @param model model description
 @return return value description
 */
- (CGSize)getImageSize:(EDSImageVideoModel *)model
{
    CGSize imgSize = CGSizeZero;
    if (self.libType == 0)
    {// 图片
//        int itemWH = (kScreenWidth - 30 - 2 * 15) / 3;
        imgSize = CGSizeMake(self.imageWH, self.imageWH);
    }
    else if (self.libType == 1)
    {// 视频
        //        EDSImageVideoModel *model = selectPhotoList[0];
        CGSize size = model.srcImage.size;
        if (size.width > size.height)
        {
            imgSize = CGSizeMake(156, 156.0 / size.width * size.height);
        }
        else
        {
            imgSize = CGSizeMake(96, 96.0 / (1.0*size.width/size.height));
        }
    }
    return imgSize;
}

- (float)imageWidth {
    if (_imageWidth) {
        return _imageWidth;
    }
    _imageWidth = (kScreenWidth - 20 * 2 - 18 * 2) / 3.0;
    return _imageWidth;
}

- (float)imageHeight {
    _imageHeight = self.imageWidth / 1.42857;
    return _imageHeight;
}


/**
 图片的视图的总高度

 @return return value description
 */
- (CGFloat)imagesHeight
{
    // 图片
    if (self.dataList.count < 3)
    {
        return self.imageHeight;
    }
    else if (self.dataList.count >= 3 && self.dataList.count < 6)
    {
        return self.imageHeight * 2 + 4;
    }
    else
    {
        return self.imageHeight * 3 + 4 * 2;
    }
    return self.imageWH;
}



/// 完成检查 开始维修
/// - Parameter completion: completion description
- (void)requestWithTaskFinishInspect:(RequestCompletionBlock)completion {
    @weakify(self);
    WDTaskFinishInspectRequest *request = [[WDTaskFinishInspectRequest alloc] initWithModel:self.reqModel];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

/// 上传图片
/// - Parameters:
///   - imageModel: imageModel description
///   - completion: completion description
- (void)requestWithUploadImage:(EDSImageVideoModel *)imageModel completion:(RequestCompletionBlock)completion {
    @weakify(self);
    
    WDUploadFileRequest *request = [[WDUploadFileRequest alloc] initWithFiles:@[imageModel]];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            WDUploadFileRspModel *rspModel = respond.data;
            if (self.addImageModel) {
                self.addImageModel.url = rspModel.link;
            }
            
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

- (void)requestWithUploadImages:(NSArray<EDSImageVideoModel *> *)images completion:(RequestCompletionBlock)completion {
    @weakify(self);
    
    WDUploadFileRequest *request = [[WDUploadFileRequest alloc] initWithFiles:images];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
//            WDUploadFileRspModel *rspModel = respond.data;
//            if (self.addImageModel) {
//                self.addImageModel.url = rspModel.link;
//            }
            
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

@end
